package io.vincentwinner.systeminfoapi;

import cn.hutool.core.text.CharSequenceUtil;
import io.vincentwinner.toolset.console.banner.ConsoleBanner;
import io.vincentwinner.toolset.console.banner.style.BannerFont;
import io.vincentwinner.toolset.console.color.ConsoleColor;
import io.vincentwinner.toolset.console.color.factory.ColorTextFactory;
import io.vincentwinner.toolset.console.color.factory.DefaultColorTextFactory;
import io.vincentwinner.toolset.console.color.factory.IntelliJIDEAColorTextFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.context.annotation.Bean;

import static io.vincentwinner.toolset.console.color.ConsoleColor.*;
import static java.lang.Boolean.parseBoolean;

@SpringBootApplication
public class SystemInfoApiApplication {

    public static void main(String[] args) {
        printBanner();
        SpringApplication.run(SystemInfoApiApplication.class,args);
    }

    /**
     * 利用虚拟机选项 port=端口号 的方式来快速改变服务端口号
     * 默认端口号 14785
     * <pre>
     *     例子: 将本程序挂载到 1478 端口
     *     java -Dport=1478 -jar systeminfo-api-xxx.jar
     * </pre>
     */
    @Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory(){
        String portStr = System.getProperty("port");
        int port;
        try{
            port = portStr == null ? 14785 : Integer.parseInt(portStr);
        }catch (NumberFormatException e){
            throw new ExceptionInInitializerError("JVM 参数 'port' 值错误 :" + portStr);
        }
        return new UndertowServletWebServerFactory(port);
    }

    /**
     * 自定义横幅显示
     */
    private static void printBanner(){
        String banner = getOrDefault(System.getProperty("banner.text"),"info api");
        String bannerType = getOrDefault(System.getProperty("banner.type"),"idea");
        String bannerFont = getOrDefault(System.getProperty("banner.font"),"3D-ASCII");
        String bannerFgColor = getOrDefault(System.getProperty("banner.fgColor"),"cyan");
        String bannerBgColor = getOrDefault(System.getProperty("banner.bgColor"),"default");
        String fgBright = getOrDefault(System.getProperty("banner.fgBright"),"true");
        String bgBright = getOrDefault(System.getProperty("banner.bgBright"),"false");
        ColorTextFactory colorTextFactory = null;
        switch (bannerType){
            case "console" : colorTextFactory =
                    new DefaultColorTextFactory(
                            text2Color(bannerFgColor),
                            text2Color(bannerBgColor),
                            parseBoolean(fgBright),
                            parseBoolean(bgBright)
                    );break;
            case "idea" :
            default: colorTextFactory =
                    new IntelliJIDEAColorTextFactory(
                            text2Color(bannerFgColor),
                            text2Color(bannerBgColor),
                            parseBoolean(fgBright),
                            parseBoolean(bgBright)
                    );
        }
        System.out.print(colorTextFactory.colorText(
                CharSequenceUtil.trimEnd(
                        ConsoleBanner.create(banner, BannerFont.getOrDefault(bannerFont,BannerFont.THREE_D_ASCII))
                )
        ));
    }

    private static String getOrDefault(String src, String defaultText){
        return src == null ? defaultText : src;
    }

    private static ConsoleColor text2Color(String color){
        ConsoleColor consoleColor = null;
        try{
            consoleColor = valueOf(color.toUpperCase());
        }catch (IllegalArgumentException e){
            consoleColor = CYAN;
        }
        return consoleColor;
    }

}
